package com.xu.redis;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import jakarta.annotation.Resource;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * redis自动装配
 *
 * @Author: 许乾伟
 * @Date: 2024/2/28 17:36
 */
public class EnableAutoConfiguration {
    @Resource
    RedisProperties properties;
    @Bean("newRedisTemplate")
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public RedisTemplate<Object, Object> redisTemplate(@Qualifier("newRedisFactory") LettuceConnectionFactory connectionFactory) {
        // 创建RedisTemplate对象
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        // 创建RedisTemplate对象
        template.setConnectionFactory(connectionFactory);
        // 创建JSON序列化器（两种设置Value和HashValue的JSON的序列化器）

        GenericFastJsonRedisSerializer fastJsonRedisSerializer
                = new GenericFastJsonRedisSerializer();
        // 设置Key和HashKey采用String序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置Value和HashValue采用JSON的序列化
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        return template;
    }
    @Bean("newRedisFactory")
    @Primary
    public LettuceConnectionFactory lettuceConnectionFactory(){
        RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
        standaloneConfiguration.setHostName(properties.getHost());
        standaloneConfiguration.setPort(properties.getPort());
        standaloneConfiguration.setDatabase(properties.getDatabase());
        standaloneConfiguration.setUsername(properties.getUsername());
        standaloneConfiguration.setPassword(properties.getPassword());
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(properties.getLettuce().getPool().getMaxIdle());
        genericObjectPoolConfig.setMinIdle(properties.getLettuce().getPool().getMinIdle());
        genericObjectPoolConfig.setMaxWaitMillis(properties.getLettuce().getPool().getMaxWait().toMillis());
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(properties.getTimeout().toMillis()))
                .poolConfig(genericObjectPoolConfig)
                .build();
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(standaloneConfiguration,clientConfig);
        return connectionFactory;
    }

}
